package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.TxnSensitiveCfgMapper;
import cc.rengu.oltp.service.common.entity.TxnSensitiveCfg;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易敏感信息配置表操作接口实现类
 * Created by wangcheng on 2021/2/25 17:10
 */
public class TxnSensitiveCfgMapperImpl implements TxnSensitiveCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public TxnSensitiveCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public List<TxnSensitiveCfg> loadTxnSensitiveCfg(String dbPoolCacheNodeName) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCacheNodeName, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadTxnSensitiveCfg");
        int pageIndex = 0;
        List<TxnSensitiveCfg> txnSensitiveCfgList = null;
        while (true) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex++);
            pageInfo.setPageSize(DbsUtil.MAX_FETCH_NUM);
            PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, new TxnSensitiveCfg(), TxnSensitiveCfg.class, pageInfo);
            if (null != pageResult) {
                if (null == txnSensitiveCfgList) {
                    txnSensitiveCfgList = new ArrayList<>();
                }
                txnSensitiveCfgList.addAll(pageResult.getResult().stream().map(item -> (TxnSensitiveCfg) (item)).collect(Collectors.toList()));
            } else {
                rglog.warn("获取交易敏感信息配置信息失败,dbPoolName:<{}>", dbPoolName);
                break;
            }
            if (pageIndex >= pageInfo.getTotalPage()) {
                break;
            }
        }
        return txnSensitiveCfgList;
    }

    @Override
    public int insertTxnSensitiveCfg(TxnSensitiveCfg txnSensitiveCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTxnSensitiveCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnSensitiveCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记交易敏感信息配置失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,txnReqType:<{}>,sensitiveField:<{}>,iReturnCode:<{}>",
                    txnSensitiveCfg.getInstId(), txnSensitiveCfg.getMsgSrcId(), txnSensitiveCfg.getTxnNum(), txnSensitiveCfg.getBizType(), txnSensitiveCfg.getOprType(), txnSensitiveCfg.getSensitiveField(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateTxnSensitiveCfgByPrimaryKey(TxnSensitiveCfg txnSensitiveCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTxnSensitiveCfgByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnSensitiveCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新交易敏感信息配置失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,txnReqType:<{}>,sensitiveField:<{}>,iReturnCode:<{}>",
                    txnSensitiveCfg.getInstId(), txnSensitiveCfg.getMsgSrcId(), txnSensitiveCfg.getTxnNum(), txnSensitiveCfg.getBizType(), txnSensitiveCfg.getOprType(), txnSensitiveCfg.getSensitiveField(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public TxnSensitiveCfg selectTxnSensitiveCfgByPrimaryKey(String instId, String msgSrcId, String txnNum, String bizType, String oprType, String sensitiveField) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnSensitiveCfgByPrimaryKey");
        TxnSensitiveCfg txnSensitiveCfg = new TxnSensitiveCfg();
        txnSensitiveCfg.setInstId(instId);
        txnSensitiveCfg.setMsgSrcId(msgSrcId);
        txnSensitiveCfg.setTxnNum(txnNum);
        txnSensitiveCfg.setBizType(bizType);
        txnSensitiveCfg.setOprType(oprType);
        txnSensitiveCfg.setSensitiveField(sensitiveField);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnSensitiveCfg, txnSensitiveCfg);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询交易敏感信息配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,oprType:<{}>,sensitiveField:<{}>,iReturnCode:<{}>",
                    instId, msgSrcId, txnNum, bizType, oprType, sensitiveField, iReturnCode);
            return null;
        }
        return txnSensitiveCfg;
    }

    @Override
    public List<TxnSensitiveCfg> selectTxnSensitiveCfgByTxnNum(String instId, String msgSrcId, String txnNum, String bizType, String oprType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnSensitiveCfgByTxnNum");
        TxnSensitiveCfg txnSensitiveCfg = new TxnSensitiveCfg();
        txnSensitiveCfg.setInstId(instId);
        txnSensitiveCfg.setMsgSrcId(msgSrcId);
        txnSensitiveCfg.setTxnNum(txnNum);
        txnSensitiveCfg.setBizType(bizType);
        txnSensitiveCfg.setOprType(oprType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnSensitiveCfg, TxnSensitiveCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("交易没有配置交易敏感信息,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,oprType:<{}>", instId, msgSrcId, txnNum, bizType, oprType);
            return null;
        }
        return objectList.stream().map(item -> (TxnSensitiveCfg) (item)).collect(Collectors.toList());
    }
}
